package pure.engine.core 
{
	import flash.utils.Dictionary;
	import pure.debug.Logger;
	import pure.engine.core.ns_despair;
	import pure.utils.DesU;

	use namespace ns_despair;
	
	/**
	 * 处理器抽象类
	 * [Author] - PURE
	 * 
	 * [Feature] - ① 新加入的进程，放在下一次更新时插入，防止混乱
	 *             ② 
	 * 
	 */
internal class ProcessorCore implements IProcessor
{
	

	
	/**
	 * 加入进程侦听
	 * 
	 * [Hint] - 全部新加入的进程侦听器，由下一帧执行...
	 * 
	 * [Param] - listener  进程侦听器
	 * [Param] - priority  优先级
	 */
	public function addProcessListener(listener:IProcessListener, priority:int = 0.0):void
	{
		if (m_lookupMap[listener])
		{
			Logger.reportError(this, '_addProcessListener', '加入了重复的侦听.');
		}

		var P:ProcessObject    =  ProcessObject.getProcessObject();
		P.listener             =  listener;
		P.priority             =  priority;
		m_lookupMap[listener]  =  P;
		
		cachedWaitingList.push(P);
		Logger.reportMessage(this, '加入侦听器...' + DesU.getClassName(listener));
	}

	
	/**
	 * 移除进程侦听
	 * 
	 * [Param] - listener  进程侦听器
	 * [Return] - 进程数量
	 */
	public function removeProcessListener(listener:IProcessListener):void
	{
		var P:ProcessObject = m_lookupMap[listener];
		
		if (!P)
		{
			Logger.reportError(this, 'removeObject', '不存在的进程侦听');
		}
		
		// 队外
		if (!P.enqueued)
		{
			cachedWaitingList.splice(cachedWaitingList.indexOf(P), 1);
		}
		// 队中
		else if(P.prev || P.next)
		{
			if (P.prev)
			{
				P.prev.next = P.next;
			}
			// 队首
			else 
			{
				m_first = P.next;
			}
			
			if (P.next)
			{
				P.next.prev = P.prev;
			}
		}
		// 仅一个
		else
		{
			m_first = null;
		}
		
		delete m_lookupMap[listener];
		P.recycle();
		Logger.reportMessage(this, '移除侦听器...' + DesU.getClassName(listener));
	}
	
	
	/**
	 * 更新
	 * 
	 * [Param] - deltaTime  推进的时间
	 */
	public function advance(deltaTime:int):void
	{
		var l:int;
		var P:ProcessObject;
		
		if((l = cachedWaitingList.length) > 0)
		{
			while (--l > -1)
			{
				P = cachedWaitingList[l];
				P.enqueued = true;
				_insertListener(P);
			}
			cachedWaitingList.length = 0;
		}
	}
	
	
	/**
	 * 插入侦听器
	 * 
	 * [Param] - [P]
	 */
	private function _insertListener(P:ProcessObject):void
	{
		var Pt:ProcessObject;
		var priority:Number;

		// 无侦听
		if (!m_first)
		{
			m_first = P;
			return;
		}
		
		// 优先级最高
		if (P.priority >= m_first.priority)
		{
			m_first.prev  =  P;
			P.next        =  m_first;
			m_first       =  P;
			return;
		}
		
		Pt        =  m_first;
		priority  =  P.priority;
		
		while(Pt.next && priority < Pt.priority)
		{
			Pt = Pt.next;
		}

		if (Pt.next)
		{
			Pt.next.prev = P;
			P.next = Pt.next;
		}
		
		Pt.next  =  P;
		P.prev   =  Pt;
	}
	

	
	
	
	
	//======================
	// Member
	//======================
	
	
	ns_despair var cachedWaitingList:Vector.<ProcessObject> = new Vector.<ProcessObject>();	// 待加入的侦听对象列表
	
	ns_despair var m_lookupMap:Dictionary = new Dictionary();  // listener : processObject
	
	ns_despair var m_first:ProcessObject;
	
}

}